package dian.qing.li.demo.security.config.security;

import com.alibaba.fastjson.JSON;
import dian.qing.li.demo.security.commons.RestResult;
import dian.qing.li.demo.security.constants.Constants;
import dian.qing.li.demo.security.utils.JwtUtils;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 注销成功
 *
 * @author: liqingdian
 **/
public class AppLogoutSuccessHandler implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 清除令牌
        JwtUtils.cancel(request);
        response.setHeader("Authorization", "");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        response.setStatus(HttpServletResponse.SC_OK);
        RestResult succeed = RestResult.succeed("注销成功", null);
        PrintWriter out = response.getWriter();
        out.write(JSON.toJSONString(succeed, Constants.FASTJSON_SERIALIZER));
        out.flush();
        out.close();
    }
}
